<?php include (dirname(__FILE__)."/../include/header.php"); ?>
<?php if(!isset($_SESSION['_user'])){header("location:../user_login.php");}?>
<?php
 //========================
 // 1 定义分页变量
 	$page = isset($_GET["page"])?$_GET["page"]:1;					//当前页号
	$pageSize = 20;				//页面大小	
	$maxRows;					// 最大数据条
	$maxPages;					// 最大页数
 	
 
 // 2获取最大数据条数
 	$sql = "select count(*) from zy_gift_delivery where user_id = '".$_SESSION['_user']['user_id']."'";
	$res = mysql_query($sql);
	$maxRows = mysql_result($res,0,0);//定位获取总条数
 
 
 // 3计算出共计最大页数
 	$maxPages=ceil($maxRows/$pageSize);
	$init = 1;
	$max = $maxPages;
	$pagelen = 10;
	// $pagelen = ($pagelen%2)?$pagelen:$pagelen+1;
	$pageoffset = 4;
	if($maxPages>$pagelen){
		if($page<=$pageoffset)
		{
			// echo "~~~~~~~~";
			$init=1;
			$max = $pagelen;
		}else{
			if($page+$pageoffset>=$maxPages+1){
				$init = $maxPages-$pagelen+1;
				// echo "!!!!!!!!!!";
			}else{
				// echo "????????????";
				$init = $page-$pageoffset;
				$max=$page+$pageoffset+1;
			}	
		}
	
	}
	
	
 
 // 4 效验当前页数
 	if($page>$maxPages){
		$page=$maxPages;
	}
	if($page<1){
		$page=1;
	}
 
 
 
 //5 拼装分页sql语句片段
	$limit = " limit ".(($page-1)*$pageSize).",{$pageSize}"; 
	$sql="SELECT * from zy_gift_delivery where user_id = '".$_SESSION['_user']['user_id']."' {$limit} ";
	$rs=mysql_query($sql);
	$total_favo=mysql_num_rows($rs);
 
?>
<style type="text/css">
<!--
a:link {
 text-decoration: none;
}
a:visited {
 text-decoration: none;
}
a:hover {
 text-decoration: none;
}
a:active {
 text-decoration: none;
}
-->
</style>
<div class="container">
<ul class="breadcrumb">
  <li><a href="<?php echo SITE_ROOT; ?>">返回首页</a><span class="divider">/</span></li>
 <li><a href="<?php echo SITE_ROOT."/user/favo_store.php"; ?>">我收藏的美食</a></li>
</ul>
<div class="row">
  <div class="span12 wash-paper profile">
    <div class="row">
      <div class="span3">
        <?php include (dirname(__FILE__)."/../include/user/left.php"); ?>
        <div class="span9">
          <div class="main-content">
            <div class="content-header">
              <h3>已换礼品表</h3>
            </div>
            <div class="content-inner">
              <table class="favor_table table table-striped">
                <thead>
                  <tr>
                    <th>礼品名称</th>
                    <th>用户名</th>
                    <th>兑换时间</th>
                  </tr>
                </thead>
                <tbody>
                  <?php while($gift=mysql_fetch_array($rs)){?>
                  <tr class="favor_food" >
                    <td><?php 
					$sql3="select * from zy_gift where gift_id ='".$gift['gift_id']."' ";
					$result3=mysql_query($sql3);
					while($zy_gift=mysql_fetch_array($result3)){
						echo $zy_gift['gift_name'];
					}
					?></td>
                    <td><?php  $sql2="select * from zy_user where user_id='".$_SESSION['_user']['user_id']."'";
					$result2=mysql_query($sql2);
					$user=mysql_fetch_array($result2);
					echo $user['username'];
					
					?></td>
                    <td><?php echo $gift['Conversiontime'];?></td>
                  </tr>
                  <?php } ?>
                </tbody>
              </table>
			  <center>
			  <?php
	if($page>1){
	if($page!=1){
  echo "<a href='giftexchanged.php?page=1'>首页</a> ";
  echo "<a href='giftexchanged.php?page=".($page-1)."'>上一页</a> ";
 }
 for($i=$init;$i<=$max;$i++){?>
  <a href="giftexchanged.php?page=<?php echo $i;?>;" <?php echo $i==$page ? "style='text-decoration: underline;font-weight: bold;'":''?> ><?php echo $i;?></a>
 <?php 
 }
 if($page!=$maxPages){
  echo "<a href='giftexchanged.php?page=".($page+1)."'>下一页</a> ";
   echo "<a href='giftexchanged.php?page={$maxPages}'>末页</a> ";
 }
 }
?>
</center>
              <div> </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <a id="back-to-top" href="javascript:void(0);">
  <div class="backtotop"></div>
  </a> </div>
<?php include (dirname(__FILE__)."/../include/footer.php"); ?>
<script type="text/javascript">
$(document).ready(function(){
	$(".admin_del").click(function(){
		var row=$(this).parent().parent();
		$.ajax({
            type: "post",
            url: "../include/zy_function.php",
			data:{method:"unFavoFood",food_id:$(this).attr("foodId")},
            dataType: "json",
            success: function(){
            	row.remove();
            	$("#total-favo-food").html($("#total-favo-food").html()-1);
            }
         });
	});
});
</script>